package com.subway.babynote;

/*水*/
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;

import com.subway.babynote.syncdata.ClientDataRecord.usersRecord;
import com.subway.babynote.syncdata.StringUtil;
import com.subway.babynote.syncservice.syncapi;

import android.widget.DatePicker;
import android.widget.TextView;
import android.database.Cursor;
import android.widget.LinearLayout;

public class birth extends Activity implements OnClickListener {
	private static final int INVISIBLE = 0x00000004;
	static final int RG_REQUEST = 0;
	private int my_Year;
	private int my_Month;
	private int my_Day;
	DatePicker my_datePicker;
	Calendar my_Calendar;
	private Button login_button;
	private Button next_button;
	NotedbHelper notedb;
	Cursor cur;
	int col;
	Intent In;
	private LinearLayout linearLayout, statistics, sync, more;
	Date date;
	int birth;

	public void onCreate(Bundle savedInstanceState) {

		my_Calendar = Calendar.getInstance(Locale.CHINA);
		/* 从日历对象中获取当前的：年、月、日、时、分 */
		date = my_Calendar.getTime();
		super.onCreate(savedInstanceState);
		setContentView(R.layout.birth);
		my_datePicker = (DatePicker) findViewById(R.id.my_DatePicker);
		login_button = (Button) findViewById(R.id.login_button);
		next_button = (Button) findViewById(R.id.next_button);
		In = new Intent();
		// initTabFunction();
		notedb = new NotedbHelper(this);
		try {
			if (syncapi.getSyncImpl().getUserid() != -1) {
				login_button.setVisibility(INVISIBLE);
				login_button.setClickable(false);
			}
			usersRecord ur = syncapi.getSyncImpl().getUser();
			if (ur != null) {
				date = StringUtil.int2date(ur.birth);
				Log.v("birth", "dateint " + ur.birth);
			} else {
				//syncapi.getSyncImpl().getVisitorid()
				
			}
		} catch (Exception e) {
			Log.v("birth", "from DB e=" + e.getMessage());
		}

		if (date.before(my_Calendar.getTime())) {
			my_Calendar.setTime(date);
		}
		my_Year = my_Calendar.get(Calendar.YEAR);
		my_Month = my_Calendar.get(Calendar.MONTH);
		my_Day = my_Calendar.get(Calendar.DAY_OF_MONTH);
		my_datePicker.init(my_Year, my_Month, my_Day,
				new DatePicker.OnDateChangedListener() {
					@Override
					public void onDateChanged(DatePicker view, int year,
							int monthOfYear, int dayOfMonth) {
						// TODO Auto-generated method stub
						/* 日期改变事件处理 */
					}
				});

		next_button.setOnClickListener(this);
		login_button.setOnClickListener(this);

	}

	public void onClick(View v) {
		if (v.equals(next_button)) {

			my_Year = my_datePicker.getYear();
			my_Month = my_datePicker.getMonth();
			my_Day = my_datePicker.getDayOfMonth();
			date = new GregorianCalendar(my_Year, my_Month, my_Day, 0, 0)
					.getTime();
			birth = (int) (date.getTime() / 1000);
			Bundle bundle = new Bundle();
			bundle.putInt("birth", birth);
			In.setClass(birth.this, gender.class);
			In.putExtras(bundle);
			startActivityForResult(In, RG_REQUEST);

		} else if (v.equals(login_button)) {

			In.setClass(birth.this, login.class);
			startActivity(In);
		}
	}

	public void onDestroy() {
		notedb.close();
		super.onDestroy();

	}

}
